package com.oa.modules.sys.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.oa.modules.sys.entity.SysDeptEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 部门表
 *
 * @author sunhui
 * @email 707022627@qq.com
 * @date 2019-10-10 18:23:00
 */
@Mapper
public interface SysDeptDao extends BaseMapper<SysDeptEntity> {

    /**
     * 查询部门管理数据
     * @param sysDept
     * @return
     */
    public List<SysDeptEntity> selectDeptList(SysDeptEntity sysDept);

    /**
     * 修改子元素关系
     * @param childrens
     */
    void updateDeptChildren(@Param("depts") List<SysDeptEntity> childrens);

    /**
     * 查询公司介绍
     * @param deptId
     */
    @Select("<script> select context,image,image_html_url,address,mobile,detailed" +
            " FROM sys_dept" +
            " WHERE 1=1 " +
            "<if test='#{deptId} != null'>" +
            " and dept_id = #{deptId}" +
            "</if>" +
            "</script>")
    SysDeptEntity qryDeptIntroduce(@Param("deptId") String deptId);

    /**
     * 查询商铺下的订单数量
     */
    @Select("<script> SELECT sd.dept_name as `name`, sum(case when so.ORDER_STATUS not in ('0','8') then 1 else 0 end ) as `value`" +
            " FROM sys_dept sd LEFT JOIN shop_order so ON so.DEPT_ID = sd.dept_id" +
            " WHERE 1=1 " +
            " <when test = 'map.dept_name != null'> and sd.dept_name LIKE '%${map.dept_name}%' </when> " +
            " GROUP BY sd.dept_id" +
            " ORDER BY value desc " +
            " <when test = 'map.dept_name == null'> limit 0,${map.top} </when>" +
            "</script>")
    List<Map<String,Object>> qryShopView(@Param("map") Map<String,Object> map);


}
